package com.rzjm.api.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.rzjm.api.constant.ResponseCode;
import com.rzjm.api.entity.SysUser;
import com.rzjm.api.entity.User;
import com.rzjm.api.entity.WxModule;
import com.rzjm.api.service.*;
import com.rzjm.api.util.GsonResultBean;
import com.rzjm.api.util.Tools;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@Author: ZhouWei
 *@Description:  微信小程序模块
 *@Date 2018-05-10
 */
@Component
@RequestMapping("/wx/mp")//url:模块/资源/{}/细分
public class WxMpController extends BaseController {

    @Resource
    private WxTemplateService wxTemplateService;

    @Value("${web.appid_mp_cy}")
    private String appidMpCy;//图片地址

    @Value("${web.secret_mp_cy}")
    private String secretMpCy;//图片地址

    /**
     * 获取公众号的accessToken
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/getAccessToken",method = {RequestMethod.POST},produces="application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject getAccessToken(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        Map<String,Object> map = new HashMap<>();
        try{
            logBefore(logger, "接收的参数： "+requestParam);
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int type = jsonRequest.containsKey("type") ? jsonRequest.getInt("type") : 0;

            String accessToken = "";
            if(type == 1){//琤一公众号
            }else if(type == 2){//fsr

            }
            accessToken = wxTemplateService.getAccessToken(appidMpCy,secretMpCy,type);
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功",accessToken),true,false);
        }catch (Exception e){
            e.printStackTrace();
            result = Tools.getJson(new GsonResultBean<>(-1,"系统异常",e.getMessage()),true,true);
            System.out.println(e.getMessage());
            logger.error(e.getMessage(), e);
        }
        return result;
    }
}
